<?php
declare(strict_types=1);

/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/30
 * Time: 17:32
 */

namespace App\Task;

use App\Model\Notify;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;

use App\Model\User;

/**
 * @Crontab(name="Foo", rule="* * * * *", callback="execute", memo="这是一个示例的定时任务")
 */
class PushBirthdayTask
{
    public function execute():void
    {
        $userList = User::whereNotNull('auth_time')->selectRaw('id,party_id,political_birthday')->get();
        if($userList->isNotEmpty()){
            $today = date('Y-m-d',time());
            foreach ($userList as $key=>$value){
                if($value->political_birthday){
                    $birthday = date('Y-m-d',strtotime($value->political_birthday));
                    if($today==$birthday){
                        $where['module'] = 1;
                        $where['uid'] = $value->id;
                        $where['relation_id'] = -1;
                        $birthdayInfo = Notify::where($where)->selectRaw('id')->first();
                        if(empty($birthdayInfo)){
                            //推送一条 党建生日的消息通知 module = 'birthday'
                            $notifyData['party_id'] = $value->party_id;
                            $notifyData['module'] = 2;
                            $notifyData['relation_id'] = -1;
                            $notifyData['uid'] = $value->id;
                            Notify::create($notifyData);
                        }
                    }
                }
            }
        }
    }
}
